<?php
	function f300($sql){
		//data handle instance
		//set request
		//define format
		//set format
		//define query string
		//process time
		//process fitler
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("300");

		$action = getValue($_GET['action']);

		if ($action == "getItems"){
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "silo_DayLevels_view","day_date_formatted");
				$query = getJSONQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else if($action == "getYears"){
			$query = getYears();
			print($dataHandle->printJSON($sql, $query));
		}
		else{
			$temp = array();
			$temp['error'] = "300: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);

	}

	function getJSONQuery(&$startTime, &$endTime, &$filter){
		$queryString = "
			SELECT
				day_date_formatted,
				day_name,
				silo_name,
				day_start_level,
				day_end_level,
				usage,
				delivered,
				start_time,
				end_time
			FROM silo_DayLevels_view ";

		$queryString = $queryString.$filter;
		
		$oderByFiled = getValue($_GET['sortItem']);
		$oderByValue = getValue($_GET['ascDesc']);

		if($oderByFiled !== "" and $oderByValue !== "")
			$queryString = $queryString." order by ".$oderByFiled." ".$oderByValue;


		return $queryString;
	}
	function getYears(){
		$queryString = 
			"SELECT DISTINCT 
				DATEPART(year, day_date) AS availableYear 
			FROM silo_DayLevels_view 
			ORDER BY availableYear desc";
		return $queryString;
	}
?>